package com.bytedance.article.common.a;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.channels.FileLock;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;


public final class MonitorHeaderInfoUtil {
    private static String a;
    private static com.bytedance.article.common.a.aa b;
    private static String c;
    private static String d;
    private static int e;
    private static int f;
    private static String g;
    private static Context h;

    static {
        a = null;
        c = null;
        d = "";
        e = 1;
        f = 0;
        g = null;
    }

    private synchronized static String a(Context arg7) {
        String v2;
        String v0_2 = null;
        SharedPreferences v4;
        String v1;
        Class v3 = MonitorHeaderInfoUtil.class;
        try {
            v1 = Settings.Secure.getString(arg7.getContentResolver(), "android_id");
            if (v1 != null) {
                if (!v1.equals("9774d56d682e549c") && v1.length() >= 13) {
                    return v1;
                }
            }

            v4 = arg7.getSharedPreferences("snssdk_openudid", 0);
            v0_2 = v4.getString("openudid", null);
            if (c(v0_2)) {
                return v0_2;
            }

            v2 = new BigInteger(64, new SecureRandom()).toString(16);
            if (v2.charAt(0) == 45) {
                v2 = v2.substring(1);
            }

            int v0_3 = 13 - v2.length();
            if (v0_3 > 0) {
                StringBuilder v5 = new StringBuilder();
                while (v0_3 > 0) {
                    v5.append('F');
                    --v0_3;
                }

                v5.append(v2);
                v2 = v5.toString();
            }

            v0_2 = a("openudid.dat", v2);
            if (!c(v0_2)) {
                v0_2 = v2;
            }
            try {
//                label_62:
                SharedPreferences.Editor v2_1 = v4.edit();
                v2_1.putString("openudid", v0_2);
                v2_1.commit();
            } catch (Exception ex) {
                Logger.w("com.bytedance.article.common.monitor.MonitorHeaderInfoUtil", "exception when making openudid: " + ex);
                label_76:
                v0_2 = v1;
            }
        } catch (Exception v0_1) {
            Logger.w("com.bytedance.article.common.monitor.MonitorHeaderInfoUtil", "exception when getting ANDROID_ID: " + v0_1);
        }
        return v0_2;
    }

    private static String a(String arg8, String arg9) {
        RandomAccessFile v7;
        FileLock v1_1 = null;
        FileLock v2_2;
        RandomAccessFile v2_1 = null;
        RandomAccessFile v1 = null;
        int v5 = 129;
        if (!"mounted".equals(Environment.getExternalStorageState())) {
            return arg9;
        }

        String v0 = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.snssdk.api/cache";
        String v2 = v0 + "/" + arg8;
        try {
            File v3 = new File(v0);
            if (!v3.exists() && !v3.mkdirs()) {
                return arg9;
            }

            File v0_3 = new File(v2);
            v2_1 = new RandomAccessFile(v0_3, "rwd");
            v1_1 = v2_1.getChannel().lock();

            if (v0_3.isFile()) {
                byte[] v3_1 = new byte[129];
                int v4 = v2_1.read(v3_1, 0, 129);
                if (v4 > 0 && v4 < v5) {
                    v0 = new String(v3_1, 0, v4, "UTF-8");
                    if (c(v0)) {
                        v2_1.close();
                        if (v1_1 != null) {
                            try {
                                v1_1.release();
                            } catch (Exception v1_2) {
                            }
                        }
                        try {
                            v2_1.close();
                        } catch (Exception v1_2) {
                        }

                        return v0;
                    }
                }
            }
            byte[] v0_4 = arg9.getBytes("UTF-8");
            v2_1.setLength(0);
            v2_1.write(v0_4);
            v2_1.close();
            if (v1_1 == null) {
//                goto label_64;
            } else {
                try {
                    v1_1.release();
                } catch (Exception v0_2) {
                }
            }
            try {
                label_64:
                v2_1.close();
            } catch (Exception v0_2) {
            }
        } catch (Exception v0_2) {
            label_121:
            v7 = v2_1;
            v2_2 = v1_1;
            v1 = v7;
            label_70:
            Logger.d("com.bytedance.article.common.monitor.MonitorHeaderInfoUtil", "load openudid exception " + v0_2);
            if (v2_2 == null) {
//                    goto label_79;
            } else {
                try {
                    v2_2.release();
                } catch (Exception ex) {
                }
            }

            if (v1 == null) {
                return arg9;
            }

            try {
                v1.close();
            } catch (Exception ex) {
            }

            return arg9;
        }


        return arg9;
    }

    public static void a(int arg0) {
        f = arg0;
    }

    public static void a(com.bytedance.article.common.a.aa arg0) {
        b = arg0;
    }

    public static void a(String arg0) {
        c = arg0;
    }

    @SuppressLint("MissingPermission")
    public static boolean a(JSONObject arg12, Context arg13) {
        String v5_3;
        String v2_1;
        String v3_1;
        Object v0_5;
        String v8;
        int v5_1;
        int v0_3;
        String v0_1;
        int v10 = 4;
        int v3 = -12;
        String v1 = null;
        h = arg13;
        HashMap v6 = new HashMap();
        try {
            v0_1 = arg13.getPackageName();
            Bundle v5 = arg13.getPackageManager().getApplicationInfo(v0_1, PackageManager.GET_META_DATA).metaData;
            if (c == null || c.length() <= 0) {
                ((Map) v6).put("channel", v5.getString("UMENG_CHANNEL"));
            } else {
                ((Map) v6).put("channel", c);
            }

            ((Map) v6).put("appkey", v5.getString("UMENG_APPKEY"));
            ((Map) v6).put("package", arg13.getPackageName());
            if (b != null) {
                d = b.a();
            }

            if (!android.support.design.a.isTextEmpty((String) null)) {
                d = null;
            }

            ((Map) v6).put("app_version", d);
            if (b != null) {
                e = b.b();
            }

            PackageInfo v0_2 = arg13.getPackageManager().getPackageInfo(v0_1, 0);
            if (v0_2.applicationInfo != null) {
                v0_3 = v0_2.applicationInfo.labelRes;
                if (v0_3 > 0) {
                    arg12.put("display_name", arg13.getString(v0_3));
                }
            }

            if (b != null) {
                v0_3 = b.c();
                if (v0_3 > 0) {
                    arg12.put("update_version_code", v0_3);
                }

                v0_3 = b.d();
                if (v0_3 <= 0) {
//                    goto label_69;
                } else {
                    arg12.put("manifest_version_code", v0_3);
                }
            }
        } catch (Exception v0) {
            boolean v0_4 = false;
            return v0_4;
        }

//        label_69:
        String[] v7 = new String[v10];
        v7[0] = "channel";
        v7[1] = "appkey";
        v7[2] = "package";
        v7[3] = "app_version";
        try {
            arg12.put("aid", f);
            v5_1 = 0;
            while (true) {
                label_84:
                if (v5_1 >= v10) {
                    try {
                        label_103:
                        arg12.put("version_code", e);
                        arg12.put("sdk_version", 1);
                        arg12.put("os", "Android");
                        arg12.put("os_version", Build.VERSION.RELEASE);
                        arg12.put("os_api", Build.VERSION.SDK_INT);
                        arg12.put("device_model", Build.MODEL);
                        arg12.put("device_brand", Build.BRAND);
                        arg12.put("device_manufacturer", Build.MANUFACTURER);
                        v0_1 = android.support.design.a.isTextEmpty(g) ? "" : g;
                        arg12.put("release_build", v0_1);
                        DisplayMetrics v2 = arg13.getResources().getDisplayMetrics();
                        switch (v2.densityDpi) {
                            case 120: {
                                v0_1 = "ldpi";
                                break;
                            }
                            case 240: {
                                v0_1 = "hdpi";
                                break;
                            }
                            case 320: {
                                v0_1 = "xhdpi";
                                break;
                            }
                            default: {
                                v0_1 = "mdpi";
                                break;
                            }
                        }

                        arg12.put("display_density", v0_1);
                        arg12.put("resolution", v2.heightPixels + "x" + v2.widthPixels);
                        v0_1 = arg13.getResources().getConfiguration().locale.getLanguage();
                        if (!android.support.design.a.isTextEmpty(v0_1)) {
                            arg12.put("language", v0_1);
                        }

                        v0_1 = android.support.design.a.d(arg13);
                        if (!android.support.design.a.isTextEmpty(v0_1)) {
                            arg12.put("mc", v0_1);
                        }

                        v0_3 = TimeZone.getDefault().getRawOffset() / 3600000;
                        if (v0_3 < v3) {
                            v0_3 = v3;
                        }

                        if (v0_3 > 12) {
                            v0_3 = 12;
                        }

                        arg12.put("timezone", v0_3);
                        arg12.put("access", android.support.design.a.doGetNetworkAccessType(android.support.design.a.getNetworkType(arg13)));
                        v0_5 = h.getSystemService(Context.TELEPHONY_SERVICE);
                        v3_1 = ((TelephonyManager) v0_5).getDeviceId();
                        v2_1 = ((TelephonyManager) v0_5).getNetworkOperatorName();
                        v0_1 = ((TelephonyManager) v0_5).getNetworkOperator();
                        v1 = v2_1;
                        v2_1 = v3_1;

                        StringBuilder v3_2 = new StringBuilder();
                        if (ToolUtils.isMiui()) {
                            v3_2.append("MIUI-");
                        } else {
                            v5_3 = ToolUtils.getEmuiInfo();
                            if (ToolUtils.b(v5_3)) {
                                v3_2.append("EMUI-");
                            }

                            if (TextUtils.isEmpty(((CharSequence) v5_3))) {
//                    goto label_193;
                            } else {
                                v3_2.append(v5_3).append("-");
                            }
                        }

//            label_193:
                        v3_2.append(Build.VERSION.INCREMENTAL);
                        v5_3 = a(h);
                        String v6_1 = b(h);
                        if (!android.support.design.a.isTextEmpty(v2_1)) {
                            arg12.put("udid", v2_1);
                        }

                        if (!android.support.design.a.isTextEmpty(v1)) {
                            arg12.put("carrier", v1);
                        }

                        if (!android.support.design.a.isTextEmpty(v0_1)) {
                            arg12.put("mcc_mnc", v0_1);
                        }

                        if (!android.support.design.a.isTextEmpty(v6_1)) {
                            arg12.put("clientudid", v6_1);
                        }

                        if (!android.support.design.a.isTextEmpty(v5_3)) {
                            arg12.put("openudid", v5_3);
                        }

                        if (v3_2.length() <= 0) {
//                goto label_226;
                        } else {
                            a = v3_2.toString();
                            arg12.put("rom", a);
                        }
                    } catch (JSONException v0_6) {
                        Logger.w("com.bytedance.article.common.monitor.MonitorHeaderInfoUtil", "prepareUDID exception: " + v0_6);
                    }

                    try {
                        label_226:
                        v0_1 = h.getSharedPreferences("applog_stats", 0).getString("user_agent", null);
                        if (android.support.design.a.isTextEmpty(v0_1)) {
                            return true;
                        }

                        arg12.put("user_agent", v0_1);
                    } catch (Exception v0) {
                    }

                    return true;
                }

                v8 = v7[v5_1];
                v0_5 = ((Map) v6).get(v8);
                if (!android.support.design.a.isTextEmpty(((String) v0_5))) {
                    arg12.put(v8, v0_5);
                    ++v5_1;
                    continue;
                }

                return false;
            }
        } catch (Exception v0) {
            v0.printStackTrace();
            return false;
        }
    }

    private synchronized static String b(Context arg5) {
        String v1;
        String v0_2;
        SharedPreferences v3;
        v3 = arg5.getSharedPreferences("snssdk_openudid", 0);
        v0_2 = v3.getString("clientudid", null);
        if (c(v0_2)) {
            return v0_2;
        }

        v1 = UUID.randomUUID().toString();
        v0_2 = a("clientudid.dat", v1);
        if (!c(v0_2)) {
            v0_2 = v1;
        }
        try {
//            label_16:
            SharedPreferences.Editor v1_1 = v3.edit();
            v1_1.putString("clientudid", v0_2);
            v1_1.commit();
        } catch (Exception v0_1) {
            label_23:
            Logger.w("com.bytedance.article.common.monitor.MonitorHeaderInfoUtil", "exception when making client_udid: " + v0_1);
            v0_2 = "";
        }
        return v0_2;
    }

    public static void b(String arg0) {
        g = arg0;
    }

    private static boolean c(String arg5) {
        boolean v0 = false;
        if (arg5 != null) {
            int v2 = arg5.length();
            if (v2 >= 13 && v2 <= 128) {
                int v1;
                for (v1 = 0; v1 < v2; ++v1) {
                    int v3 = arg5.charAt(v1);
                    if ((v3 < 48 || v3 > 57) && (v3 < 97 || v3 > 102) && (v3 < 65 || v3 > 70) && v3 != 45) {
                        return v0;
                    }
                }

                v0 = true;
            }
        }

        return v0;
    }
}

